<template>
  <van-button
    v-if="!isFollow"
    type="info"
    round
    size="small"
    icon="plus"
    @click="follow"
    :loading="isLoading"
    >关注</van-button
  >
  <van-button v-else round size="small" @click="follow" :loading="isLoading"
    >已关注</van-button
  >
</template>

<script>
import { addFollow, delFollow } from '@/api/user'
export default {
  props: ['article'],
  data () {
    return {
      isFollow: this.article.is_followed,
      isLoading: false
    }
  },

  created () {},

  methods: {
    async follow () {
      this.isLoading = true
      try {
        if (this.isFollow) {
          // 已关注  取消关注
          await delFollow(this.article.aut_id)
          this.$toast('取消关注成功！')
          this.isFollow = false
        } else {
          // 未关注 添加关注
          await addFollow(this.article.aut_id)
          this.$toast('关注成功！')
          this.isFollow = true
        }
      } catch (error) {
        if (error.response.status === 400) {
          this.$toast('不允许关注自己啦！')
        }
        console.log(error)
      }
      this.isLoading = false
    }
  }
}
</script>

<style scoped lang="less"></style>
